package com.jprocms.module.cms.es.enums;

import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jprocms
 */
public enum EnumContentSort {
    DEFAULT(0, "默认排序（置顶在前，再按发布时间降序）", null, null, false),

    RELEASE_TIME_DESC(1, "按照发布时间降序", "release_time", "releaseTime", false),

    RELEASE_TIME_ASC(2, "按照发布时间升序", "release_time", "releaseTime", true),

    CREATE_TIME_DESC(3, "按照创建时间降序", "create_time", "createTime", false),

    CREATE_TIME_ASC(4, "按照创建时间升序", "create_time", "createTime", true),

    VIEWS_DESC(6, "总访问量降序", "views", "views", false),

    VIEWS_ASC(7, "总访问量升序", "views", "views", true),

    COMMENTS_DESC(8, "总评论量降序", "comments", "comments", false),

    COMMENTS_ASC(9, "总评论量升序", "comments", "comments", true),

    UPS_DESC(10, "总点赞量降序", "ups", "ups", false),

    UPS_ASC(11, "总点赞量升序", "ups", "ups", true),

    DOWNS_DESC(12, "总点踩数量降序", "downs", "downs", false),

    DOWNS_ASC(13, "总点踩数升序", "downs", "downs", true),

    DOWNLOADS_DESC(14, "总下载量降序", "downloads", "downloads", false),

    DOWNLOADS_ASC(15, "总下载量升序", "downloads", "downloads", true),

    ;

    private Integer sort;
    private String desc;
    private String column;
    private String field;
    private boolean isAsc;

    EnumContentSort(Integer sort, String desc, String column, String field, boolean isAsc) {
        this.sort = sort;
        this.desc = desc;
        this.column = column;
        this.field = field;
        this.isAsc = isAsc;
    }

    public static EnumContentSort getContentSort(Integer sort) {
        return Arrays.stream(EnumContentSort.values())
                .filter(item -> item.sort.equals(sort)).findFirst().orElse(null);
    }

    /**
     * 数据库的排序转换es的搜索排序
     *
     * @param orderItem
     * @return
     */
    public static EnumContentSort getContentSort(OrderItem orderItem) {
        if (orderItem == null) {
            return EnumContentSort.DEFAULT;
        }
        if (orderItem.getColumn().contains(RELEASE_TIME_DESC.getField())) {
            if (orderItem.isAsc()) {
                return RELEASE_TIME_ASC;
            }
            return RELEASE_TIME_DESC;
        } else if (orderItem.getColumn().contains(CREATE_TIME_DESC.getField())) {
            if (orderItem.isAsc()) {
                return CREATE_TIME_ASC;
            }
            return CREATE_TIME_DESC;
        } else if (orderItem.getColumn().contains(VIEWS_DESC.getField())) {
            if (orderItem.isAsc()) {
                return VIEWS_ASC;
            }
            return VIEWS_DESC;
        } else if (orderItem.getColumn().contains(COMMENTS_DESC.getField())) {
            if (orderItem.isAsc()) {
                return COMMENTS_ASC;
            }
            return COMMENTS_DESC;
        } else if (orderItem.getColumn().contains(DOWNLOADS_DESC.getField())) {
            if (orderItem.isAsc()) {
                return DOWNLOADS_ASC;
            }
            return DOWNLOADS_DESC;
        } else if (orderItem.getColumn().contains(UPS_DESC.getField())) {
            if (orderItem.isAsc()) {
                return UPS_ASC;
            }
            return UPS_DESC;
        } else if (orderItem.getColumn().contains(DOWNS_DESC.getField())) {
            if (orderItem.isAsc()) {
                return DOWNS_ASC;
            }
            return DOWNS_DESC;
        }
        return EnumContentSort.DEFAULT;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public boolean isAsc() {
        return isAsc;
    }

    public void setAsc(boolean asc) {
        isAsc = asc;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public static Map<Integer, String> getSortMap() {
        return Arrays.stream(EnumContentSort.values()).collect(
                Collectors.toMap(EnumContentSort::getSort, EnumContentSort::getDesc));
    }
}
